﻿using System.Collections.Generic;
using System.Linq;

namespace Hourglass.Qrobot.Models
{
    public sealed class DisscussesManager
    {
        public Dictionary<long, Disscuss> Disscusses { get; set; }

        public DisscussesManager()
        {
            this.Disscusses = new Dictionary<long, Disscuss>();
        }

        public DisscussMember FindMemberFromDisscussByName(string nickName, long disscussId)
        {
            if (Disscusses.ContainsKey(disscussId))
            {
                return Disscusses[disscussId].FindMemberByName(nickName);
            }
            return null;
        }

        public DisscussMember FindMemberFromDisscussById(long id, long disscussId)
        {
            if (Disscusses.ContainsKey(disscussId))
            {
                return Disscusses[disscussId].FindMemberById(id);
            }
            return null;
        }

        public DisscussMember FindMemberFromDisscussByQq(long qq, long disscussId)
        {
            if (Disscusses.ContainsKey(disscussId))
            {
                return Disscusses[disscussId].FindMemberByQq(qq);
            }
            return null;
        }

        public Disscuss FindDisscussByName(string name)
        {
            return Disscusses.Values.FirstOrDefault(d => d.Name.Equals(name));
        }
    }

    public sealed class Disscuss
    {
        public long Id { get; set; }

        public string Name { get; set; }

        public Dictionary<long, DisscussMember> Members { get; set; }

        public Disscuss()
        {
            this.Members = new Dictionary<long, DisscussMember>();
        }

        public Disscuss(string name) : this()
        {
            this.Name = name;
        }

        public DisscussMember FindMemberByName(string nickName)
        {
            return Members.Values.FirstOrDefault(d => d.NickName.Equals(nickName));
        }

        public DisscussMember FindMemberById(long id)
        {
            if (Members.ContainsKey(id))
            {
                return Members[id];
            }
            return null;
        }

        public DisscussMember FindMemberByQq(long qq)
        {
            return Members.Values.FirstOrDefault(d => d.Qq == qq);
        }
    }

    public sealed class DisscussMember : IPerson
    {
        private string _nickName;

        public long Id { get; set; }

        public long Qq { get; set; }

        public Disscuss BelongDisscuss { get; set; }

        public string NickName
        {
            get
            {
                if (string.IsNullOrEmpty(_nickName))
                {
                    return Id.ToString();
                }
                return _nickName;
            }
            set { _nickName = value; }
        }

        public long Ruin { get; set; }

        public SmartEnums.OnlineClientType ClientType { get; set; }

        public SmartEnums.OnlineStatus Status { get; set; }

        public QqMessage.PersonInfo GetPersonInfo()
        {
            return new QqMessage.PersonInfo
            {
                //Id = Id,
                Qq = Qq,
                NickName = NickName,
                MarkName = NickName
            };
        }
    }
}
